<template>
  <el-dialog
    :title="info.isAdd ? '活动添加' : '活动编辑'"
    :visible.sync="info.isshow"
    @closed="closed"
  >
    <el-form :model="user">
      {{ user }}
      {{time}}
      <el-form-item label="活动名称" label-width="120px">
        <el-input v-model="user.title" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="活动名称" label-width="120px">
        <el-date-picker
          v-model="time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="timestamp"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="一级分类" label-width="120px">
        <el-select
          v-model="user.first_cateid"
          placeholder="请选择上级菜单"
          @change="changeFirst"
        >
          <!-- 遍历菜单列表 -->
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类" label-width="120px">
        <el-select
          v-model="user.second_cateid"
          placeholder="请选择上级菜单"
          @change="changeGoods"
        >
          <!-- 遍历菜单列表 -->
          <el-option
            v-for="item in secondCateList"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品" label-width="120px">
        <el-select v-model="user.goodsid" placeholder="请选择上级菜单">
          <!-- 遍历菜单列表 -->
          <el-option
            v-for="item in goods"
            :key="item.id"
            :label="item.goodsname"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" label-width="120px">
        <el-switch
          v-model="user.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
      <el-button type="primary" v-else @click="update">修 改</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqseckadd,
  reqcatelist,
  reqseckedit,
  reqseckinfo,
  reqgoodslist,
  reqgoodsinfo
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  // 4.接收info
  props: ["info"],
  data() {
    return {
      //2.1表单数据
      //   2.2 数据绑定到页面，一一对应到每一个表单
      list: [],
      secondCateList: [],
      goods: [],
      user: {
        id: "",
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      },
      time: "",
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/list",
    }),
    newList() {
      return this.list.filter((item) => item.id !== this.user.id);
    },
  },
  methods: {
    ...mapActions({}),
    // 5.点了取消
    cancel() {
      this.info.isshow = false;
    },
    // 7.一级分类变了，开始计算二级分类的列表
    changeFirst() {
      // console.log(cateList);
      //二级分类的值先清空
      this.user.second_cateid = "";
      //根据一级分类的id号 请求二级分类的列表
      this.getSecondList();
    },
    //计算二级列表
    async getSecondList() {
      //根据一级分类的id号 请求二级分类的列表
      let res = await reqcatelist({ pid: this.user.first_cateid });
      if (res.data.code === 200) {
        this.secondCateList = res.data.list ? res.data.list : [];
      }
    },
    async changeGoods() {
      let res = await reqgoodslist({
        fid: this.user.first_cateid,
        sid: this.second_cateid,
      });
      if (res.data.code === 200) {
        // console.log("你好");
        // this.secondCateList = res.data.list ? res.data.list : [];
        this.goods = res.data.list ? res.data.list : [];
        // console.log(this.goods);
      }
    },
    //清空user
    empty() {
      this.user = {
        id: "",
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      };
    },
    //2.3添加
    // 2.4 响应拦截统一做了失败处理
    add() {
      this.user.begintime = this.time[0];
      this.user.endtime = this.time[1];
      this.time = [];
      console.log(this.user)
      reqseckadd(this.user).then((res) => {
        if (res.data.code === 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 清空user的数据
          this.empty();
          // 弹框消失
          this.info.isshow = false;
          // 刷新列表
          this.$emit("init");
        }
      });
    },
    //修改了pid
    changePid() {
      // 如果pid是0，那么type=1;否则，type=2
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqseckinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.user = res.data.list;
          // 补id
          this.user.id = id;
          this.time = [this.user.begintime,this.user.endtime]
          console.log("time");
            console.log(this.time);
          //根据1级分类id计算二级列表
          this.getSecondList();
          reqgoodsinfo({id:this.user.goodsid}).then(res=>{
            // console.log(res);
            this.goods = [{goodsname:res.data.list.goodsname}]
          })
        }
      });
    },
    //修改
    update() {
      reqseckedit(this.user).then((res) => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 清空user的数据
          this.empty();
          // 弹框消失
          this.info.isshow = false;
          // 刷新列表
          this.time = [];
          this.$emit("init");
        }
      });
    },
    closed() {
      
      //如果是编辑的关闭，就要执行empty
      if (!this.info.isAdd) {
        this.empty();
        this.time = [];
      }
    },
  },
  mounted() {
    reqcatelist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.list = res.data.list;
        // console.log(this.list);
      }
    });
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
</style>